package com.zcl.logic;

import java.io.IOException;

import com.netqin.ui.share.sinaweibo.AsyncWeiboRunner.RequestListener;
import com.netqin.ui.share.sinaweibo.WeiboException;
import com.zcl.bean.Task;
import com.zcl.protocol.APPProBase;
import com.zcl.utils.NqLog;

import android.content.Context;
import android.os.Handler;

public class WeiboService implements RequestListener{
	private Context mContext;
	private Handler mHandler;
	private APPProBase mpAppProBase;
	
	public WeiboService(Context context)
	{
		mContext = context;
	}
	
	public void requestWeiboProtocol(Handler handler, APPProBase appProBase)
	{
		mHandler = handler;
		mpAppProBase = appProBase;
		
		execute();
	}
	
	private void execute()
	{
		mpAppProBase.addRequestListener(this);
		mpAppProBase.generateRequestData(mContext);
	}
	
	@Override
	public void onComplete(String response) {
		NqLog.d("onComplete"+response);
		boolean blog = mpAppProBase.parseResponseData(mContext, response);
		if (blog) {
			mHandler.sendMessage(mHandler.obtainMessage(Task.PARSE_SUCCESS, mpAppProBase.getProtocolType(), 0, mpAppProBase.getParseData()));
		}else {
			mHandler.sendMessage(mHandler.obtainMessage(Task.PARSE_FAILED, mpAppProBase.getProtocolType(), 0, mpAppProBase.getParseData()));
		}
	}
	@Override
	public void onIOException(IOException e) {
		NqLog.d("onIOException");
		mHandler.sendMessage(mHandler.obtainMessage(Task.PARSE_FAILED, mpAppProBase.getProtocolType(), -1));
		
	}
	@Override
	public void onError(WeiboException e) {
		NqLog.d("onError"+e.getStatusCode());
		mHandler.sendMessage(mHandler.obtainMessage(Task.PARSE_FAILED, mpAppProBase.getProtocolType(), e.getStatusCode()));
	}
}
